import mongoose from 'mongoose';

let connection = null;

export default async function main(uri) {
    connection = await mongoose.connect(uri);
    
    // use `await mongoose.connect('mongodb://user:password@127.0.0.1:27017/test');` if your database has auth enabled

    mongoose.connection.once('close' , () => {
        console.log('Connection closed');
        main();
    })

    return connection;
}

export function getConnection() {
    return connection;
}

export function closeConnection() {
    return mongoose.disconnect();
}
